package kr.co.handysoft.project.smartPolice.dialog;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImageViewDialog extends Dialog {

	protected Object result;
	protected Shell shlImageview;
	private String imgPath;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ImageViewDialog(Shell parent, int style, String imagePath) {
		super(parent, style);
		this.imgPath = imagePath;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlImageview.open();
		shlImageview.layout();
		Display display = getParent().getDisplay();
		
        Rectangle shellBounds = getParent().getBounds();
        Point dialogSize = shlImageview.getSize();

        shlImageview.setLocation(shellBounds.x + (shellBounds.width - dialogSize.x) / 2, shellBounds.y + (shellBounds.height - dialogSize.y) / 2);

		
		while (!shlImageview.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlImageview = new Shell(getParent(), getStyle());
		shlImageview.setSize(972, 514);
		shlImageview.setText("Image View");
		shlImageview.setLayout(new FillLayout());
		
		Canvas canvas = new Canvas(shlImageview, SWT.NONE);
		canvas.addPaintListener(new PaintListener() {
		      public void paintControl(PaintEvent e) {
		        Image image = new Image(shlImageview.getDisplay(), imgPath);

		        e.gc.drawImage(image, 0, 0);

		        image.dispose();
		      }
		});

	}

}
